package net.gq.logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.gq.logistics.pojo.*;
import net.gq.logistics.service.BaseDataService;
import net.gq.logistics.service.CustomerService;
import net.gq.logistics.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ASUS
 * @date 2020/5/26
 */

@Controller("customerController")
@RequestMapping("/customer")
public class CutomerController {

    @Resource(name = "resultData")
    private ResultData rs;

    @Resource(name = "customerService")
    private CustomerService service;

    @Resource(name = "userService")
    private UserService userService;


    @Resource(name = "baseDataService")
    private BaseDataService baseDataService;

    @RequestMapping("/customerPage")
    public String customer() {

        return "customerPage";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageInfo<Customer> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String keyword) {

        CustomerExample example = new CustomerExample();

        //开始分页
        PageHelper.startPage(pageNum, pageSize);
        CustomerExample.Criteria criteria = example.createCriteria();

        //获取当前身份，判断当前是否是业务员，如果是业务员，则只能察看自己的客户
        Subject subject = SecurityUtils.getSubject();
        //获取认证对象
        User user = (User) subject.getPrincipal();

        System.out.println(user.toString());
        if (user.getRolename().equals("业务员")) {
            //只展示当前业务员的数据
            criteria.andRealnameEqualTo(user.getRealname());
        }




        /*搜索功能*/
        if (StringUtils.isNotBlank(keyword)) {
            //根据用户姓名和业务员进行搜索

            criteria.andCustomerNameLike("%" + keyword + "%");

        }
        //搜索所有数据
        List<Customer> customers = service.selectByExample(example);

        //回传分页数据
        return new PageInfo<Customer>(customers);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultData delete(Long customerId) {

        int result = service.deleteByPrimaryKey(customerId);

        if (result > 0) {
            rs = ResultData.createRs(1, "删除成功!");
        } else {
            rs = ResultData.createRs(0, "删除失败!");
        }

        return rs;

    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public ResultData deleteAll(String str) {

        int result = 0;
        if (StringUtils.isNotBlank(str)) {

            String[] customerIds = str.split(",");


            for (String customerId : customerIds) {

                //开始删除
                result = service.deleteByPrimaryKey(Long.valueOf(customerId));


            }

            if (result > 0) {
                rs = ResultData.createRs(1, "删除成功！");

            } else {
                rs = ResultData.createRs(0, "删除失败");
            }

        }

        //回传结果
        return rs;

    }


    @RequestMapping("/edit")
    public String edit(Model model, Long customerId) {

        UserExample example = new UserExample();

        //查询出所有角色名称为业务员的管理员
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andRoleameEqualTo("业务员");
        List<User> users = userService.selectByExample(example);


        model.addAttribute("users", users);


        //回传区间数据
        List<BaseData> sections = baseDataService.selectBaseDataByParentName("区间管理");

        model.addAttribute("sections", sections);


        //修改数据
        if (customerId != null) {
            //查询出当前用户的数据，共享给编辑页面，以供修改的回显数据
            Customer customer = service.selectByPrimaryKey(customerId);

            model.addAttribute("customer", customer);

        }


        return "customerEdit";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public ResultData insert(Customer customer) {

        int result = service.insert(customer);

        if (result > 0) {

            rs = ResultData.createRs(1, "插入成功");
        } else {
            rs = ResultData.createRs(0, "插入失败！");
        }

        return rs;

    }

    @RequestMapping("/update")
    @ResponseBody
    public ResultData update(Customer customer) {

        int result = service.updateByPrimaryKeySelective(customer);
        if (result > 0) {

            rs = ResultData.createRs(1, "更新成功！");
        } else {
            rs = ResultData.createRs(0, "更新失败！");
        }

        return rs;
    }
}
